<template>
	<view class="container">
		<view class="intro">
			<image src="/static/images/customer-level/5.png"></image>
			<view class="tips">
				遇见~ 美好~
			</view>
		</view>
		<view class="bottom">

			<!-- #ifdef H5 -->
				<button type="primary" size="default" class="login-btn" @tap="loginOnDev" :disabled="logining">
					登录(开发环境)
				</button>
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
				<button type="primary" size="default" class="login-btn" open-type="getUserInfo" @getuserinfo="weiXinLogin" :disabled="logining">
					<image src="/static/images/mine/wechat.png"></image>
					微信授权登录
				</button>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				logining: false
			}
		},
		onLoad(){
			this.handlerLocalToken()
		},
		methods: {
			handlerLocalToken() {
				let token = uni.getStorageSync('token');
				uni.removeStorageSync('token');
				if (token) {
					let that = this;
					that.$store.commit('setToken', token);
					that.$request('/api-app/user/info', 'get').then(result => {
						console.log('[通过token登录]');
						that.$store.commit('login', result.data);
						uni.navigateBack();
					});
				}
			},
			// 微信小程序登录 通过code在服务器获取微信openid登录
			async weiXinLogin(e) {
				const that = this;
				that.logining = true;
				const {errMsg, userInfo} = e.detail
				if(errMsg !== "getUserInfo:ok") {
					uni.showModal({
						title: '提示',
						content: '您取消了授权登录，请重新授权',
						showCancel: false
					})
					return
				}
				
				// let weixUserInfo = e.detail.userInfo // {avatarUrl, city, country, gender, nickname...}
				uni.login({
					provider: 'weixin',
					success: wxres => {
						that.logining = false;
						// eg: {code: "073BdF1w3us1mV2SRk1w3MDxpH3BdF1z", errMsg: "login:ok"}
						console.log('微信登录 获取到的code=', wxres.code);
						that.$request('/api-app/user/login/weixin', 'post', {
							code: wxres.code,
							wxAvatar: e.detail.userInfo.avatarUrl
						}).then(result => {
							result.data.wxAvatar = e.detail.userInfo.avatarUrl; // 同步微信头像
							that.$store.commit('login', result.data);
							uni.navigateBack();
						});
					}
				});
			},
			async loginOnDev() {
				let that = this;
				console.log("开发环境，登录")
				let path = '/api-app/user/login/dev';
				that.$request(path, 'post').then(result => {
					that.$store.commit('login', result.data);
					uni.navigateBack();
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.intro {
		width: 100%;
		height: 50vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-evenly;
		font-size: $font-size-base;
		color: $text-color-assist;
		
		image {
			width: 200rpx;
			height: 200rpx;
		}
		
		.tips {
			line-height: 72rpx;
			text-align: center;
		}
	}
	
	.bottom {
		height: 20vh;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 0 40rpx;
		
		.login-btn {
			width: 100%;
			border-radius: 50rem !important;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 10rpx 0;
			
			image {
				width: 36rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}
		}
		
		.row {
			.grid {
				width: 20%;
				image {
					width: 60rpx;
					height: 60rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>
